package com.egao.orderB.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.CustomerBiz;
import com.egao.orderB.biz.CustomerMaintainFeeBiz;
import com.egao.orderB.biz.MessageBiz;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.CustomerMaintainFee;
import com.egao.orderB.entity.Message;
import com.egao.orderB.service.CustomerMaintainFeeService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

//import com.egao.orderB.mapper.CustomerMaintainFeeMapper;
//import javax.transaction.Transactional;

/**
 * 货代运费维护服务实现类
 * @author : Doman
 * @date : 2021-07-15 14:12:35
 * @version ：1.0
 */
@Service
public class CustomerMaintainFeeServiceImpl /*extends ServiceImpl<CustomerMaintainFeeMapper, CustomerMaintainFee> */implements CustomerMaintainFeeService {

	@Autowired
	private CustomerMaintainFeeBiz customerMaintainFeeBiz;
	@Autowired
	private CustomerBiz customerBiz;
	@Autowired
	private MessageBiz messageBiz;

    /**
    @Override
    public PageResult<CustomerMaintainFee> listPage(PageParam<CustomerMaintainFee> page) {
        List<CustomerMaintainFee> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<CustomerMaintainFee> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, CustomerMaintainFee customerMaintainFee, User loginUser)  throws Exception{
        return customerMaintainFeeBiz.list(page,customerMaintainFee,loginUser);
    }

    @Override
    public JsonResult list(CustomerMaintainFee customerMaintainFee, User loginUser) {
        return JsonResult.ok().setData(customerMaintainFeeBiz.list(customerMaintainFee,loginUser));
    }

    @Override
    public JsonResult getById(CustomerMaintainFee customerMaintainFee) {
        return JsonResult.ok().setData(customerMaintainFeeBiz.getById(customerMaintainFee));
    }

    @Transactional
    @Override
    public JsonResult save(CustomerMaintainFee customerMaintainFee, User loginUser) {
        if (customerMaintainFeeBiz.save(customerMaintainFee, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(CustomerMaintainFee customerMaintainFee, User loginUser) {
        if (customerMaintainFeeBiz.update(customerMaintainFee, loginUser)) {
            messageBiz.save(new Message(customerMaintainFee.getAddress()+customerMaintainFee.getBoxType()+customerMaintainFee.getPrice(),
                    customerMaintainFee.getCustomerId(), "customerFee", ""), loginUser);
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(CustomerMaintainFee customerMaintainFee, User loginUser) {
        if (customerMaintainFeeBiz.remove(customerMaintainFee, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Transactional
    @Override
    public JsonResult importBatch(MultipartFile file, User loginUser) {
//        StringBuilder sb = new StringBuilder();
        try {
            // 读取excel
            int startRow = 1;
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream(), 0);
            List<List<Object>> list = reader.read(startRow);
            // 进行非空和重复检查
//            sb.append(CoreUtil.excelCheckBlank(list, startRow, 0, 1, 2, 3, 4));
//            sb.append(CoreUtil.excelCheckRepeat(list, startRow, 0, 1, 2, 3));
//            if (!sb.toString().isEmpty()) return JsonResult.error(sb.toString());
            // 进行数据库层面检查
            int okNum = 0, errorNum = 0;
            for (int i = 0; i < list.size(); i++) {
                List<Object> objects = list.get(i);
                String customerName = String.valueOf(objects.get(0));  // 客户
                String address = String.valueOf(objects.get(1));  // 地址
                String boxType = String.valueOf(objects.get(2));  // 柜型
                String price = String.valueOf(objects.get(3));  // 价格
                String dock = String.valueOf(objects.get(4));  // 港口
                Customer c1 = new Customer();
                c1.setName(customerName);
                List<Customer> customers = customerBiz.getByName(c1);
                if(customers != null && customers.size() == 1) {
                    c1 = customers.get(0);
                    CustomerMaintainFee customerMaintainFee = new CustomerMaintainFee();
                    customerMaintainFee.setCustomerId(Integer.valueOf(c1.getId()));
                    customerMaintainFee.setAddress(address);
                    customerMaintainFee.setBoxType(boxType);
                    customerMaintainFee.setPrice(price);
                    customerMaintainFee.setDock(dock);
                    customerMaintainFeeBiz.save(customerMaintainFee,loginUser);
                    okNum++;
                }
            }
            return JsonResult.ok("导入完成，成功" + okNum + "条，失败" + errorNum + "条");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JsonResult.error("导入失败");
    }

}
